package com.azxc.rapid.plus.system.service.impl;

import com.azxc.rapid.core.secure.utils.AuthUtil;
import com.azxc.rapid.core.tool.node.ForestNodeMerger;
import com.azxc.rapid.core.tool.utils.Func;
import com.azxc.rapid.core.tool.utils.StringUtil;
import com.azxc.rapid.plus.system.mapper.DeptPlusMapper;
import com.azxc.rapid.plus.system.service.IDeptPlusService;
import com.azxc.rapid.plus.system.vo.TreeNodeVO;
import com.azxc.rapid.plus.user.service.IUserPlusService;
import com.azxc.rapid.system.entity.Dept;
import com.azxc.rapid.system.entity.Region;
import com.azxc.rapid.system.user.vo.UserVO;
import com.azxc.rapid.system.vo.DeptVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 */
@Service
public class DeptPlusServiceImpl extends ServiceImpl<DeptPlusMapper, Dept> implements IDeptPlusService {

	private static final String TENANT_ID = "tenantId";

	private static final String PARENT_ID = "parentId";

	@Autowired
	private IUserPlusService userService;

	@Value("${supervise.canSendSameLevel:false}")
	private boolean canSendSameLevel;

	/**
	 * 根据当前用户的部门id,查看当前部门以及部门下的节点
	 *
	 * @return
	 */
	@Override
	public List<TreeNodeVO> getTreeByUser() {
		UserVO userVoInfo = userService.getUserVoInfo();
		String regionCode = "";
		String deptId = "";
		if (userVoInfo != null) {
			regionCode = userVoInfo.getRegionCode();
			Integer userLevel = userVoInfo.getUserLevel();
			if (userLevel == 2) {
				//说明是最高级别
				DeptVO deptVO = baseMapper.getDeptByCode(regionCode);
				deptId = deptVO.getId() + "";
			} else if (userLevel == 4) {//最底层
				deptId = userVoInfo.getDeptId();
			} else {
				//不是最高级别
				DeptVO deptVO = baseMapper.getDeptByCode(regionCode);
				deptId = deptVO.getId() + "";
			}
		}
		List<TreeNodeVO> list = ForestNodeMerger.merge(baseMapper.getTreeByUser(deptId));
		return list;
	}

	/**
	 * 根据部门Id,查询部门对应的行政区域-对应的部门list
	 *
	 * @param deptId
	 * @return
	 */
	@Override
	public List<DeptVO> getListDeptById(String deptId) {
		Region regionVO = baseMapper.getListDeptById(deptId);
		String ancestors = regionVO.getAncestors();
		String code = regionVO.getCode();
		List<String> codes = new ArrayList<>();
		codes.add(code);
		List<String> strs = Func.toStrList(ancestors);
		codes.addAll(strs);
		List<DeptVO> list = baseMapper.getListDeptByRegionCodes(codes);
		return list;
	}


	@Override
	public Dept selectDeptByUser(Long userId) {
		Dept dept = null;
		try {
			dept = baseMapper.selectById(userService.userInfo(userId).getUser().getDeptId());
		} catch (Exception e) {
			return dept;
		}
		return dept;
	}

	@Override
	public List<TreeNodeVO> getDeptTreeByUser() {
		UserVO userVoInfo = userService.getUserVoInfo();
		String deptId = userVoInfo.getDeptId();
		List<TreeNodeVO> list = ForestNodeMerger.merge(baseMapper.getTreeByUser(deptId));
		return list;
	}

	@Override
	public List<TreeNodeVO> getDeptLazyTreeByDeptId(String deptId) {
		List<TreeNodeVO> list = new ArrayList<>();
		if(StringUtil.isNotBlank(deptId)){
			//传递部门ID，则查询部门的下级
			list = baseMapper.getLazyTreeByUser(deptId);
		}else{
			//如果不传部门ID，则查询当前用户部门
			String userDeptId = userService.getUserVoInfo().getDeptId();
			list = baseMapper.getCurrentDept(userDeptId);
		}
		return list;
	}

	@Override
	public List<TreeNodeVO> ChildDeptByUser(String deptName) {
		String deptId = userService.getUserVoInfo().getDeptId();
		List<TreeNodeVO> list = ForestNodeMerger.merge(baseMapper.ChildDeptByUser(deptId,deptName));
		return list;
	}

	@Override
	public List<DeptVO> ChildDeptByDeptId(String deptId) {
		List<DeptVO> list = baseMapper.ChildDeptByDeptId(deptId);
		return list;
	}

	@Override
	public List<TreeNodeVO> ChildDeptByUserRegionCode() {
		UserVO userVoInfo = userService.getUserVoInfo();
		String deptId = userVoInfo.getDeptId();
		String regionCode =userVoInfo.getRegionCode();
		int levelLength = 0;
		switch (regionCode.length()){
			case 2 : levelLength=4; break;
			case 4 : levelLength=6; break;
			case 6 : levelLength=9; break;
			case 9 : levelLength=9; break;
		}
		List<TreeNodeVO> list = ForestNodeMerger.merge(baseMapper.ChildDeptByUserRegionCode(deptId,regionCode,levelLength));
		return list;
	}

	@Override
	public List<DeptVO> getListDeptByRegionCode(String regionCode) {
		List<String> codes = new ArrayList<>();
		codes.add(regionCode);
		List<DeptVO> list = baseMapper.getListDeptByRegionCodes(codes);
		return list;
	}

	@Override
	public List<DeptVO> deptByDeptId(String deptId) {
		List<DeptVO> list = baseMapper.deptByDeptId(deptId);
		return list;
	}

	@Override
	public Long getDeptByRegionCodeAndType(String regionCode, String type) {
		return baseMapper.getDeptByRegionCodeAndType(regionCode, type);
	}

	@Override
	public List<TreeNodeVO> selectDeptTreeWithUserGrant(String deptId) {
		List<TreeNodeVO> list = new ArrayList<>();
		//懒加载方式获取数据，如不传部门ID，判断是否管理员，是管理员则获取第一级的部门，不是则根据当前用户所在部门获取子部门
		if(StringUtil.isNotBlank(deptId)){
			list = baseMapper.getLazyTreeByUser(deptId);
		}else{
			if(AuthUtil.isAdministrator()){
				list = baseMapper.getLazyTreeByUser("0");
			}else {
				String userDeptId = userService.getUserVoInfo().getDeptId();
				list = baseMapper.getCurrentDept(userDeptId);
			}
		}
		return list;
	}

	@Override
	public List<Dept> selectDeptListByRegion(String regionCode, String deptId) {
		//行政区划级别
		/* 13 - 省级
		   1301	- 市级
		   130102 - 区级
		   130102001 - 乡镇
		   130102001001 - 居委会 一般不到这一级
		*/
		int minLength=0;
		int manxLength=0;
		if(canSendSameLevel){
			switch (regionCode.length()){
				case 2: minLength=2;manxLength=4;break;
				case 4: minLength=4;manxLength=6;break;
				case 6: minLength=6;manxLength=9;break;
				case 9: minLength=9;manxLength=11;break;
				case 11: minLength=11;manxLength=11;break;
				default:break;
			}
		}else{
			switch (regionCode.length()){
				case 2: minLength=4;manxLength=4;break;
				case 4: minLength=6;manxLength=6;break;
				case 6: minLength=9;manxLength=9;break;
				case 9: minLength=11;manxLength=11;break;
				default:break;
			}
		}

		return this.baseMapper.selectDeptListByRegion(regionCode,deptId,minLength,manxLength);
	}

	@Override
	public List<TreeNodeVO> treeByRegion(String regionCode) {
		List<TreeNodeVO> list = new ArrayList<>();
		list = ForestNodeMerger.merge(baseMapper.treeByRegion(regionCode));
		return list;
	}
}
